#r "nuget: Newtonsoft.Json, 13.0.3"

using System.Data;
using Newtonsoft.Json;

public class NatsumeSchedule
{
    public string gameweek;
    public List<DateModel> days = new List<DateModel>();

    public class DateModel
    {
        public string date;
        public EventsHolder events = new EventsHolder();
    }
    public class EventsHolder
    {
        public List<ScheduleItem> day = new List<ScheduleItem>();
        public List<ScheduleItem> evening = new List<ScheduleItem>();
        public List<ScheduleItem> night = new List<ScheduleItem>();
    }
    public class ScheduleItem
    {
        public string title;
        public string footnote;
    }
}


private NatsumeSchedule BuildModel(string inputMDFile, string gameWeekName)
{
    string[] lines = File.ReadAllLines(inputMDFile).Append("#EOF").ToArray();
    NatsumeSchedule schedule = new NatsumeSchedule();
    schedule.gameweek = gameWeekName;

    NatsumeSchedule.DateModel currentDate = null;
    List<NatsumeSchedule.ScheduleItem> currentItems = null;
    NatsumeSchedule.ScheduleItem lastItem = null;

    foreach (var item in lines)
    {
        if (string.IsNullOrWhiteSpace(item))
        {
            continue;
        }
        if (item.StartsWith("#"))
        {
            if (currentDate != null)
            {
                schedule.days.Add(currentDate);
            }
            currentDate = new NatsumeSchedule.DateModel();
            currentDate.date = item.Substring(1).Trim();
            continue;
        }
        if (item.StartsWith("-"))
        {
            var scheduleItem = new NatsumeSchedule.ScheduleItem();
            scheduleItem.title = item.Substring(1).Trim();
            lastItem = scheduleItem;
            currentItems.Add(lastItem);
        }
        else if (item.StartsWith("*"))
        {
            lastItem.footnote = item.Substring(1).Trim();
        }
        else
        {
            if (item.StartsWith("白天"))
            {
                currentItems = currentDate.events.day;
            }
            else if (item.StartsWith("傍晚"))
            {
                currentItems = currentDate.events.evening;
            }
            else if (item.StartsWith("夜晚"))
            {
                currentItems = currentDate.events.night;
            }
            else
            {
                throw new Exception("Unexpected schedule time: " + item);
            }
        }
    }
    return schedule;
}


List<NatsumeSchedule> natsumeSchedules = new List<NatsumeSchedule>();

natsumeSchedules.Add(BuildModel("../src/GameWeek1.md", "一周目"));
natsumeSchedules.Add(BuildModel("../src/GameWeek2.md", "二周目"));
natsumeSchedules.Add(BuildModel("../src/GameWeek3.md", "三周目"));
natsumeSchedules.Add(BuildModel("../src/GameWeek4.md", "四周目"));

if(!Directory.Exists("../out")){
    Directory.CreateDirectory("../out");
}

File.WriteAllText("../out/GameWeeks.json", JsonConvert.SerializeObject(natsumeSchedules, Formatting.Indented));
File.WriteAllText("../out/GameWeeks.min.json", JsonConvert.SerializeObject(natsumeSchedules, Formatting.None));
